package com.palm.core.translator;

import com.palm.core.util.StringUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MultipleTranslator implements Translator{
    final private Map<Object,Object> valueMapping;
    private String split=",";
    public MultipleTranslator(Map<Object,Object> mapping){
        this.valueMapping=mapping;
    }
    public MultipleTranslator(Map<Object,Object> mapping,String split){
        this.valueMapping=mapping;
        this.split=split;
    }
    @Override
    public Object translate(Object target,String filedName,Object value){
        if(value==null){
            return null;
        }
        String[] vs=value.toString().split(split);
        List<Object> dist=new ArrayList<>();
        for(String k:vs){
            dist.add(valueMapping.get(k));
        }
        return StringUtil.join(dist);
    }
}
